//npm install jsencrypt
import JSEncrypt from "jsencrypt";
import { emitter } from "@/utils/emitter";

//加密
export const encrypt = (txt: any): string => {
    const encryptor = new JSEncrypt();
    encryptor.setPublicKey(import.meta.env.VITE_PUBLICK_KEY); //设置公钥
    const result = encryptor.encrypt(txt); //对需要加密的数据进行加密，它的返回值是 string 或 false
    if (result) {
        return result;
    } else {
        emitter.emit("msg-error", "加密失败");
        return "";
    }
};

//解密
export const decrypt = (txt: string): string => {
    const encryptor = new JSEncrypt();
    encryptor.setPrivateKey(import.meta.env.VITE_PUBLICK_KEY); //设置公钥
    const result = encryptor.decrypt(txt); //对需要加密的数据进行解密，它的返回值是 string 或 false
    if (result) {
        return result;
    } else {
        emitter.emit("msg-error", "解密失败");
        return "";
    }
};
